﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace WxApiLib.Messages
{
    /// <summary>
    /// 
    /// </summary>
    public class IMGTextMessage : Message
    {
        /// <summary>
        /// 模板静态字段
        /// </summary>
        private static string m_Template;
        private static string s_Template;

        /// <summary>
        /// 图片链接（由系统生成）
        /// </summary>
        public string PicUrl { get; set; }

        /// <summary>
        /// 图片消息媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }
        /// <summary>
        /// 消息ID
        /// </summary>
        public string MsgId { get; set; }
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public IMGTextMessage()
        {
            this.MsgType = "image";
        }
        /// <summary>
        /// 从xml数据加载文本消息
        /// </summary>
        /// <param name="xml"></param>
        public static IMGTextMessage LoadFromXml(string xml)
        {
            IMGTextMessage tm = null;
            if (!string.IsNullOrEmpty(xml))
            {
                XElement element = XElement.Parse(xml);
                if (element != null)
                {
                    tm = new IMGTextMessage();
                    tm.FromUserName = element.Element(Common.FROM_USERNAME).Value;
                    tm.ToUserName = element.Element(Common.TO_USERNAME).Value;
                    tm.CreateTime = element.Element(Common.CREATE_TIME).Value;
                    tm.PicUrl = element.Element(Common.PIC_URL).Value;
                    tm.MsgType = element.Element(Common.MSG_TYPE).Value;
                    tm.MediaId = element.Element(Common.MEDIA_ID).Value;
                    tm.MsgId = element.Element(Common.MSG_ID).Value;
                }
            }

            return tm;
        }
        /// <summary>
        /// 模板
        /// </summary>
        public override string Template
        {
            get
            {
                if (string.IsNullOrEmpty(m_Template))
                {
                    LoadTemplate();
                }

                return m_Template;
            }
        }
        /// <summary>
        /// 生成内容
        /// </summary>
        /// <returns></returns>
        public override string GenerateContent()
        {
            this.CreateTime = Common.GetNowTime();
            string tmp=LoadSendTemplate();
            return string.Format(tmp, this.ToUserName, this.FromUserName, this.CreateTime, this.MsgType, this.MediaId);
        }
        /// <summary>
        /// 加载模板
        /// </summary>
        private static void LoadTemplate()
        {
            m_Template = @"
                            <xml>
                                 <ToUserName><![CDATA[{0}]]></ToUserName>
                                 <FromUserName><![CDATA[{1}]]></FromUserName>
                                 <CreateTime>{2}</CreateTime>
                                 <MsgType><![CDATA[image]]></MsgType>
                                 <PicUrl><![CDATA[{4}]]></PicUrl>
                                 <MediaId><![CDATA[{5}]]></MediaId>
                                 <MsgId>{6}</MsgId>
                            </xml>
                            ";
        }

        /// <summary>
        /// 加载发送模板
        /// </summary>
        private static string LoadSendTemplate()
        {
           return @"
                           <xml>
                                <ToUserName><![CDATA[{0}]]></ToUserName>
                                <FromUserName><![CDATA[{1}]]></FromUserName>
                                <CreateTime>{2}</CreateTime>
                                <MsgType><![CDATA[{3}]]></MsgType>
                                <Image>
                                <MediaId><![CDATA[{4}]]></MediaId>
                                </Image>
                          </xml>
                            ";
        }
    }
}
